package com.sciener.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.sciener.dao.CarDaoImp;
import com.sciener.dao.UserDaoImp;
import com.sciener.entity.Type;
import com.sciener.entity.User;
import com.sciener.entity.resultdata.DriverResult;
import com.sciener.entity.resultdata.LevelItem;
import com.sciener.entity.resultdata.UserDetail;
import com.sciener.entity.resultdata.Userlevel;
import com.sciener.util.LocationUtils;

public class UserService {
	private static UserDaoImp userDao = new UserDaoImp();
	
	/**
	 * ��������û�
	 * @return
	 */
	public List<User> getAllUsers()
	{
		return userDao.getAllUsers();
	}
	
	public void inserteUser(User user)
	{
		userDao.insertUser(user);
	}
	
	public int  getUseridByUsernameAndPassword(String username,String password)
	{
		return userDao.getUseridByusernameandpassword(username, password);
	}
	
	public User getUserById(int id)
	{
		return userDao.getUserbyUserId(id);
	}
	
	public void deleteUser(User user)
	{
		userDao.deleteUser(user);
	}
	
	/**
	 * �ж��û����Ƿ����
	 * @param name
	 * @return
	 */
	public boolean userNameExite(String name)
	{
		return userDao.userNameExite(name);
	}
	
	public boolean insertUser(User user)
	{
		return userDao.insertUser(user);
	}
	
	/**
	 * �������������͸������
	 * @param userId
	 * @param infoType
	 * @param value
	 */
//	public void updateUserInfo(int userId, String infoType, String value)
//	{
//		User user = userDao.getUserbyUserId(userId);
//		if(user == null)
//		{
//			return;
//		}
//		if(StringUtils.equals(infoType, "username"))
//		{
//			user.setUsername(value);
//		}else if(StringUtils.equals(infoType, "password"))
//		{
//			user.setPassword(value);
//		}else if(StringUtils.equals(infoType, "email"))
//		{
//			user.setEmail(value);
//		}else if(StringUtils.equals(infoType, "header"))
//		{
//			user.setHeader(value);
//		}else if(StringUtils.equals(infoType, "nickname"))
//		{
//			user.setNickname(value);
//		}else if(StringUtils.equals(infoType, "carid"))
//		{
//			user.setCarid(Integer.parseInt(value));
//			
//		}else if(StringUtils.equals(infoType, "licenceid"))
//		{
//			user.setLicenceid(Integer.parseInt(value));
//			
//		}else if(StringUtils.equals(infoType, "sign"))
//		{
//			user.setSign(value);
//		}else if(StringUtils.equals(infoType, "gpsw"))
//		{
//			user.setGpsw(value);
//		}else if(StringUtils.equals(infoType, "gpsh"))
//		{
//			user.setGpsw(value);
//		}else if(StringUtils.equals(infoType, "address"))
//		{
//			user.setAddress(value);
//		}else if(StringUtils.equals(infoType, "sex"))
//		{
//			user.setSex(Integer.parseInt(value));
//		}
//		
//		userDao.updateUser(user);
//	}
	
	/**
	 * �ж��û���������Ƿ���ȷ
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean rightUserAndPwd(String username, String password) {
		
		return userDao.rightUserAndPwd(username,password);
	}
	
	public static void main(String[] args) {
		UserService s = new UserService();
		UserDaoImp userDao = new UserDaoImp();
		/*boolean exite = userDao.userNameExite("liqiang");
		System.out.println("�û��Ƿ���ڣ�"+exite);
		
		boolean user = userDao.rightUserAndPwd("liqiang", "123");
		System.out.println("�û����Ƿ���ڣ�"+user);
		*/
	/*	boolean insert = userDao.insertUser("chenhao", "123");
		System.out.println("�����û���"+insert);
		boolean exiteCh = userDao.userNameExite("chenhao");
		System.out.println("����"+exiteCh);*/
//		User user1 = userDao.getUserbyUserName("chenhao");
//		int a = user1.getId();
//		s.updateUserInfo(a, "username", "ch");
//		s.updateUserInfo(a, "password", "123");
//		s.updateUserInfo(a, "sex", "1");
//		/*user1.setAddress("1234");
//		user1.setPassword("chwo123");
//		user1.setEmail("50123@qq.com");
//		user1.setNickname("i am casbo");
//		user1.setCarid(1);
//		userDao.updateUser(user1);*/
		
	//	System.out.println(userDao.getUserbyUserName("chenhao").getAddress());
		
	/*	Type type = new Type();
		type.setDescrib("�鳵����");*/
		
	/*	boolean delete1 = userDao.deleteUser(user1);*/
		
	/*	boolean exiteCh = userDao.userNameExite("chenhao");
		System.out.println(exiteCh);
		
		User user = new User();
		user.setUsername("chenhao");
		userDao.insertUser(user);
		
		boolean exiteCh1 = userDao.userNameExite("chenhao");
		System.out.println(exiteCh1);
		*/
//		boolean delete = userDao.deleteUser(user)
		/*String a = "1";
		int b = Integer.parseInt(a);
		System.out.println(b);
		*/
		
	}

	public UserDetail getrecallerdetali(Integer userid) {
		
		return userDao.getrecllerdetail(userid);
	}

	public Userlevel getuserlevelsbyid(Integer userid) {
		
		return userDao.getuserlevelsbyid(userid);
	}

	public List<LevelItem> getlevellistbyid(Integer userid) {
		
		return userDao.getlevellistbyid(userid);
	}

	public String submitjudge(String topicid, String give, String rating,
			String saysth, String whatkindoforder) {
		String result = "fail";
		try
		{
		if(whatkindoforder.equals("agent"))//如果是发帖人的评价
		{
			userDao.insertagentjudge(topicid,give,rating,saysth);
			int state = userDao.findtopicstate(topicid);
			if(state==4)
			{
				userDao.updateTopicstate(topicid,5);
			}
			else
			{
				userDao.updateTopicstate(topicid,3);
			}
			
			//还要计算一下应招人的司机好评率，然后更新用户好评率信息
			//找到这条帖子的应招人id
			int recallerid = userDao.findrecalleridByTopicid(topicid);
			if(recallerid!=0)
			{
				//计算该用户的司机评价有几条
				int count = userDao.getDriverJudgeCount(recallerid);
				int goodcount = userDao.getDriverJudgegoodCount(recallerid);
				String goodratio = goodcount*100/count+"%";
				userDao.updateDriverJudgeByuserid(recallerid,goodratio);
			}
			result="success";
		}
		else if(whatkindoforder.equals("driver"))
		{
			userDao.insertdriverjudge(topicid,give,rating,saysth);
			int state = userDao.findtopicstate(topicid);
			if(state==3)
			{
				userDao.updateTopicstate(topicid,5);
			}
			else
			{
				userDao.updateTopicstate(topicid,4);
			}
			//更新一下发帖人的代驾好评率
			int agentid = userDao.findagentidByTopicid(topicid);
			if(agentid!=0)
			{
				//计算该用户的代驾评价有几条
				int count = userDao.getAgentJudgeCount(agentid);
				int goodcount = userDao.getAgentJudgegoodCount(agentid);
				String goodratio = goodcount*100/count+"%";
				userDao.updateAgentJudgeByuserid(agentid,goodratio);
			}
			result="success";
		}
		}catch (Exception e) {
			e.printStackTrace();
			result = "fail";
			return result;
		}
		return result;
	}

	public List<LevelItem> getdriverlevellistbyid(Integer userid) {
		// TODO Auto-generated method stub
		return userDao.getdriverlevellistbyid(userid);
	}

	public String updateUserInfo(Integer userid, String prop, String content) {
		try {
			userDao.updateUserinfo(userid,prop,content);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	public String updateCarInfo(Integer userid, String prop, String content) {
		try {
			userDao.updateCarinfo(userid,prop,content);
			userDao.updateCarVerify(userid,0);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	public String updateLicenceInfo(Integer userid, String prop, String content) {
		try {
			userDao.updateLicenceinfo(userid,prop,content);
			userDao.updateLicenceVerify(userid,0);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}

	public void updateHeaderbyid(String userid, String photoPath) throws  Exception{
		userDao.updateHeaderByuserid(Integer.valueOf(userid),photoPath);
		
	}

	public void insertUserLocation(Double longtitude, Double latitude,Integer userid) {
		boolean haslocation = userDao.findLocationByuserid(userid);
		try{
		if(haslocation)
		{
			userDao.updateLocationByuserid( longtitude,  latitude, userid);
		}
		else
		{
			userDao.insertUserLocation( longtitude,  latitude, userid);
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<DriverResult> getRoundDrivers(Double longtitude, Double latitude,Integer userid) {
		double[] aroundLocations = LocationUtils.getAround(latitude, longtitude, 2000);
		List<DriverResult> roundDrivers = userDao.getRoundDrivers(aroundLocations[0],aroundLocations[1],aroundLocations[2],aroundLocations[3],longtitude,latitude,userid);
		// TODO Auto-generated method stub
		return  roundDrivers;
	}

}
